<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header > 黑马面经运营后台</template>

 <el-form :model="form" :rules="rules" ref="loginForm">
  <el-form-item label="用户名:" prop="username">

    <el-input  placeholder="请输入用户名" v-model="form.username"></el-input>
  </el-form-item>
  <el-form-item label="密码:" prop="password">

    <el-input  placeholder="请输入密码" v-model="form.password"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm">登录</el-button>
    <el-button>重置</el-button>
  </el-form-item>
 </el-form>

    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '输入正确的用户名', trigger: 'blur' },
          { min: 5, max: 15, message: '输入5-15位的字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '输入正确的密码', trigger: 'blur' },
          // 设置正则parttern
          { pattern: /^\w{5,15}$/, message: '输入5-15位的字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return
        // console.log('成功')
        const res = await login(this.form)
        console.log(res)
        this.$message({
          message: '登陆成功',
          type: 'success'

        })
        setToken(res.data.token)
        this.$router.push('/')
        this.$store.commit('buildToken')
      })
    }
  },
  created () {

  }
}
</script>

<style lang="scss" scoped>

  .login-page {
  min-height: 100vh;
  background-image: url('../../assets/login-bg.svg');
  display: flex;
  align-items: center;
  justify-content: space-around;}

  .box-card {
  width: 420px;
  margin: 100px auto;
 ::v-deep .el-card__header{
  color: white;
  text-align: center;
  background-color: #8e96f7;
  }
}
</style>
